package com.xiaohai.big_event_system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaohai.big_event_system.common.R;
import com.xiaohai.big_event_system.projo.Article;
import com.xiaohai.big_event_system.service.IArticleService;
import com.xiaohai.big_event_system.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 文章
 * 前端控制器
 *
 * @author author
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;


    /**
     * 获取文章列表
     * 列表
     *
     * @param currentPage
     * @param pageSize
     * @param article
     * @return
     */
    @GetMapping
    public R<IPage<Article>> list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, Article article) {
        IPage<Article> list = articleService.list(currentPage, pageSize, article);
        return R.success(list, "数据获取成功");
    }

    /**
     * 添加文章
     * 添加文字
     *
     * @param article
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody @Validated Article article) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        article.setCreateUser(id);
        articleService.save(article);
        return R.success(null, "添加成功");
    }


}
